package com.hongkzh.www.look.media.view.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.hongkzh.www.R;
import com.hongkzh.www.look.media.model.bean.CityBean;
import com.hongkzh.www.look.media.view.adapter.CityFirstAdapter;
import com.hongkzh.www.look.media.view.adapter.CitySecondAdapter;
import com.hongkzh.www.look.media.view.adapter.CityThirdAdapter;
import com.hongkzh.www.other.Dao.CityDao;
import com.hongkzh.www.view.activity.BaseActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;


/**
 * 城市地区筛选
 * Created by as on 2018/4/13.
 */

public class CitySelectActivity extends BaseActivity implements View.OnClickListener {


    @BindView(R.id.iv_Search)
    ImageView ivSearch;
    @BindView(R.id.iv_cancel)
    ImageView ivCancel;
    @BindView(R.id.layout_native)
    RelativeLayout layoutNative;
    @BindView(R.id.layout_international)
    RelativeLayout layoutInternational;

    @BindView(R.id.listView_FirstName)
    ListView listViewFirstName;
    @BindView(R.id.layout_First)
    FrameLayout layoutFirst;
    @BindView(R.id.listView_SecondName)
    ListView listViewSecondName;
    @BindView(R.id.layout_Second)
    FrameLayout layoutSecond;
    @BindView(R.id.listView_ThirdName)
    ListView listViewThirdName;
    @BindView(R.id.layout_Third)
    FrameLayout layoutThird;
    @BindView(R.id.layout_searchTitle)
    LinearLayout layoutSearchTitle;
    private int selectedPosition;

    private ListView subListView;
    private CityFirstAdapter mRootAdapter;
    CitySecondAdapter subAdapter;
    CityThirdAdapter mThirdAdapter;
    private String mLocationPro;

    private final int RESULTCODE = 0;

    private ArrayList<String> mProvinceNameList=new ArrayList<>();
    private ArrayList<String> mProvinceCodeList=new ArrayList<>();

    private ArrayList<String> mCityNameList = new ArrayList<>();
    private ArrayList<String> mCityCodeList = new ArrayList<>();

    private ArrayList<String> mDistrictNameList = new ArrayList<>();
    private ArrayList<String> mDistrictCodeList = new ArrayList<>();
    private CityDao cityDao;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_area_search;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ButterKnife.bind(this);
        setTranslucent(R.color.color_D8D8D8);

    }

    @Override
    protected void init() {


        cityDao=new CityDao(this);
        getProvinceData();

        layoutFirst.setVisibility(View.VISIBLE);
        layoutSecond.setVisibility(View.INVISIBLE);
        layoutThird.setVisibility(View.INVISIBLE);

        mRootAdapter = new CityFirstAdapter(this);
        mRootAdapter.setItems(getProvinceData());
        listViewFirstName.setAdapter(mRootAdapter);

        listViewFirstName.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                // TODO Auto-generated method stub

                /**
                 * 选中root某项时改变该ListView item的背景色
                 */
                mRootAdapter.setSelectedPosition(position);
                mRootAdapter.notifyDataSetInvalidated();
                mLocationPro=mRootAdapter.getItem(position);

                selectedPosition = position;
                 subAdapter = new CitySecondAdapter(
                        CitySelectActivity.this, getCityData(mProvinceCodeList.get(position)), position);
                listViewSecondName.setAdapter(subAdapter);

                /**
                 * 选中某个根节点时，使显示相应的子目录可见
                 */
                layoutSecond.setVisibility(View.VISIBLE);
                layoutThird.setVisibility(View.INVISIBLE);
                listViewSecondName
                        .setOnItemClickListener(new AdapterView.OnItemClickListener() {

                            @Override
                            public void onItemClick(
                                    AdapterView<?> parent, View view,
                                    int position, long id) {
                                // TODO Auto-generated method stu

                                subAdapter.setSelectedPosition(position);
                                subAdapter.notifyDataSetInvalidated();


                                layoutThird.setVisibility(View.VISIBLE);
                                 mThirdAdapter=new CityThirdAdapter(CitySelectActivity.this,getDistrictData(mCityCodeList.get(position)),position);
                                listViewThirdName.setAdapter(mThirdAdapter);

                                listViewThirdName.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                                    @Override
                                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                                        Intent intent = new Intent();
                                        intent.putExtra("areaName",mLocationPro+"-"+mThirdAdapter.getItem(position));
                                        CitySelectActivity.this.setResult(RESULT_OK, intent);
                                        CitySelectActivity.this.finish();
                                    }
                                });


                            }
                        });

            }
        });
    }

    @Override
    protected void setListener() {

        ivCancel.setOnClickListener(this);
        layoutSearchTitle.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.iv_cancel:
                CitySelectActivity.this.setResult(1);
                this.finish();
                break;
            case R.id.layout_searchTitle:
                // 点击跳转到搜索页面
                Intent intent=new Intent(CitySelectActivity.this,CitySearchActivity.class);
                startActivity(intent);
                break;
        }

    }

    @Override
    public void onError(Exception e) {

    }


    public ArrayList<String> getProvinceData() {

        List<CityBean> mProvinceList=cityDao.selectProvince();
        for(CityBean cityBean:mProvinceList){
            mProvinceNameList.add(cityBean.name);
            mProvinceCodeList.add(cityBean.code);
        }
        return mProvinceNameList;
    }

    private ArrayList<String> getCityData(String pcode) {
        mCityNameList.clear();
        mCityCodeList.clear();
        List<CityBean> mCityList = cityDao.selectCity(pcode);
        for (CityBean bean : mCityList) {
            mCityNameList.add(bean.name);
            mCityCodeList.add(bean.code);
        }
        return mCityNameList;
    }

    private ArrayList<String> getDistrictData(String cityCode) {
        mDistrictNameList.clear();
        mDistrictCodeList.clear();
        List<CityBean> mDistrictList = cityDao.selectDistrict(cityCode);
        for (CityBean bean : mDistrictList) {
            mDistrictNameList.add(bean.name);
            mDistrictCodeList.add(bean.code);
        }
        return mDistrictNameList;
    }

}
